import torch
import torch.nn as nn
import torch.optim as optim

# 定义一个简单的神经网络
class SimpleNN(nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc1 = nn.Linear(10, 50)
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(50, 1)

    def forward(self, x):
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc2(out)
        return out

# 实例化模型、损失函数和优化器
model = SimpleNN()
criterion = nn.MSELoss()
optimizer = optim.SGD(model.parameters(), lr=0.01)

# 训练循环
for epoch in range(num_epochs):
    model.train()
    optimizer.zero_grad()
    
    # 假设inputs和targets是已经准备好的数据
    outputs = model(inputs)
    loss = criterion(outputs, targets)
    
    # 手动添加L2正则化项
    l2_reg = sum(p.pow(2).sum() for p in model.parameters())
    loss += lambda_value * l2_reg  # lambda_value是正则化强度
    
    loss.backward()
    optimizer.step()

